/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#pragma once

#include "../IO/PrintStream.h"

namespace Hogshead
{
	namespace Common
	{
		class Vector3;
		class Matrix4;

		/*
		* Morgan Kauffman essential math and http://cache-www.intel.com/cd/00/00/29/37/293748_293748.pdf
		*/
		class Quaternion
		{
		public:
			Quaternion(void);
			Quaternion(float w, float x, float y, float z);
			Quaternion(const Vector3& axis, float angle);
			Quaternion(const Quaternion& in_source);
			Quaternion(float euler_x, float euler_y, float euler_z);
			Quaternion(const Matrix4& matrix);
			
			
			~Quaternion(void);

			const static Quaternion identity;

			Quaternion& toIdentifty();
			Quaternion& normalize();

			Quaternion getInverse() const;
			Vector3 rotate(const Vector3& in_to_transform) const;

			Quaternion& operator=(const Quaternion& in_source);
			Quaternion operator*(const Quaternion& in_right) const;
			Quaternion operator*(float in_right) const;
			friend Quaternion operator*(float in_left, const Quaternion& in_right);
			Quaternion operator+(const Quaternion& in_right) const;
			Quaternion operator/(float right) const;
			Quaternion mul(const Quaternion& in_right) const;

			static float dot(const Quaternion& left, const Quaternion& right);
			static float angleBetween(const Quaternion& left, const Quaternion& right);
			static Quaternion slerp(const Quaternion& start, const Quaternion& end, float t);
			static Quaternion intermediateSlerp( const Quaternion& low_end, const Quaternion& intermediate, const Quaternion& high_end, float t);

			Matrix4 toMatrix4() const;

			Vector3 getForward() const;
			Vector3 getUp() const;
			Vector3 getRight() const;

			Quaternion& setFromBasis(const Matrix4& matrix);
			Quaternion& setFromBasis(const Vector3& forward, const Vector3& up);
			Quaternion& setFromAxisAngle(const Vector3& axis, float angle);

			__forceinline float x() const
			{
				return _x;
			}

			__forceinline float y() const
			{
				return _y;
			}

			__forceinline float z() const
			{
				return _z;
			}

			__forceinline float w() const
			{
				return _w;
			}

			friend PrintStream& operator<<(PrintStream& in_stream, const Quaternion& in_q);

		private:
			Quaternion(float w, const Vector3& v);
			

			float _w;//w = cos(theta/2)
			float _x;//v = sine(theta/2)r
			float _y;
			float _z;

			friend class Matrix4;

			//http://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation
		};
	}
}
